package org.groupg.practice.swingx.demo;

import lombok.Getter;
import lombok.Setter;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;
import org.jdesktop.swingx.renderer.FormatStringValue;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

@Setter
@Getter
public class PersonTable extends JPanel {
    private JXTable table;
    private DefaultTableModel tableModel;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;

    // 回调接口
    public interface TableActionListener {
        void onAddRequest();
        void onEditRequest(int selectedRow);
        void onDeleteRequest(int selectedRow);
    }

    private TableActionListener listener;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public PersonTable() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        initTable();
        initButtons();
    }

    private void initTable() {
        // 表格模型
        String[] columns = {"ID", "姓名", "出生日期", "性别"};
        tableModel = new DefaultTableModel(columns, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 表格不可编辑
            }
        };

        table = new JXTable(tableModel);
        table.setColumnControlVisible(true); // 显示列控制

        // 设置日期渲染器
        table.getColumnModel().getColumn(2).setCellRenderer(
            new DefaultTableRenderer(new FormatStringValue(dateFormat))
        );

        // 设置行高亮
        table.addHighlighter(new ColorHighlighter(
            HighlightPredicate.ODD,
            new Color(240, 240, 245),
            Color.BLACK
        ));

        // 启用排序
        table.setSortable(true);

        // 添加滚动条
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(600, 300));
        add(scrollPane, BorderLayout.CENTER);
    }

    private void initButtons() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));

        addButton = new JButton("添加");
        editButton = new JButton("编辑");
        deleteButton = new JButton("删除");

        addButton.addActionListener(e -> {
            if (listener != null) listener.onAddRequest();
        });

        editButton.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            if (selectedRow >= 0) {
                if (listener != null) listener.onEditRequest(table.convertRowIndexToModel(selectedRow));
            } else {
                JOptionPane.showMessageDialog(this, "请选择要编辑的行", "提示", JOptionPane.WARNING_MESSAGE);
            }
        });

        deleteButton.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            if (selectedRow >= 0) {
                if (listener != null) listener.onDeleteRequest(table.convertRowIndexToModel(selectedRow));
            } else {
                JOptionPane.showMessageDialog(this, "请选择要删除的行", "提示", JOptionPane.WARNING_MESSAGE);
            }
        });

        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    public void setTableActionListener(TableActionListener listener) {
        this.listener = listener;
    }

    public void addPerson(int id, String name, Date birthDate, String gender) {
        tableModel.addRow(new Object[]{
            id,
            name,
            birthDate,
            gender
        });
    }

    public void updatePerson(int row, String name, Date birthDate, String gender) {
        tableModel.setValueAt(name, row, 1);
        tableModel.setValueAt(birthDate, row, 2);
        tableModel.setValueAt(gender, row, 3);
    }

    public void deletePerson(int row) {
        tableModel.removeRow(row);
    }

    public Object[] getPersonData(int row) {
        return new Object[]{
            tableModel.getValueAt(row, 0),
            tableModel.getValueAt(row, 1),
            tableModel.getValueAt(row, 2),
            tableModel.getValueAt(row, 3)
        };
    }
}